#ifndef NETWORK_ZINGME_NETWORKZINGMEWEBSOCKET_H
#define NETWORK_ZINGME_NETWORKZINGMEWEBSOCKET_H

#include <QList>
#include <QSemaphore>
#include <QThread>
#include <QVariant>


namespace Tufao {
class WebSocket;
}

namespace Network {

class ConnectionRequest;

namespace Zingme {

class Websocket : public QThread
{
    Q_OBJECT
public:
    explicit Websocket(QObject *parent = 0);

public:
    bool connect(const QString &server, int port, const QVariant &params);
    bool send(ConnectionRequest *request);
    void disconnect();

protected:
    void run();

signals:
    void connected();
    void received(QByteArray message);
    void sended(ConnectionRequest *request);
    void failed(ConnectionRequest *request);
    void disconnected();

private slots:
    void onFinished();
    void onConnected();
    void onReceived(QByteArray message);
    void onDisconnected();

private:
    void stop();

private:
    QSemaphore m_disconnector;
    QSemaphore m_locker;
    QSemaphore m_activator;
    bool m_disconnected;
    QString m_server;
    int m_port;
    QVariant m_params;
    Tufao::WebSocket *m_websocket;

    typedef QList<Network::ConnectionRequest *> Requests;
    Requests m_requests;
};

} // namespace zingme

} // namespace network

#endif // NETWORK_ZINGME_NETWORKZINGMEWEBSOCKET_H
